#!/usr/bin/env bash

CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

# running test suite is successful by default
tests_exit_value=0

test_files() {
	ls -1 $CURRENT_DIR |        # test files are in current dir
		\grep -i '^test' |       # test file names start with 'test'
		xargs                   # file names in one line
}

set_global_exit_val_to_false() {
	tests_exit_value=1
}

run_test() {
	local test_file="$1"
	local tmux_copy_mode="$2"

	# running test
	echo "Test: $test_file (copy-mode $tmux_copy_mode)"

	# by setting the EDITOR var tmux chooses vi or emacs copy mode
	EDITOR="$tmux_copy_mode" $CURRENT_DIR/$test_file

	# handling exit value
	local exit_value="$?"
	if [ "$exit_value" == 0 ]; then
		echo "Success"
	else
		echo "Test failed!"
		set_global_exit_val_to_false
	fi
	echo
}

main() {
	local test_file
	local test_dir_path="./"
	for test_file in $(test_files); do
		run_test "$test_file" "vi"
		run_test "$test_file" "emacs"
	done
	exit "$tests_exit_value"
}
main
